package com.yxq.mdt.app.modules.bottomsheet;

import android.content.DialogInterface;
import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.personal.bean.MyPersonalBean;
import com.yxq.mdt.app.modules.socialcontact.bean.LiveResultBean;
import com.yxq.mdt.app.modules.video.bean.ChannelListBean;
import com.yxq.mdt.app.modules.video.model.MainVideoModel;
import com.yxq.mdt.app.net.ApiModel;
import com.yxq.mdt.app.widgets.CircleImageView;
import com.yxq.mdt.app.widgets.tagview.Tag;
import com.yxq.mdt.app.widgets.tagview.TagView;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author L
 * @time 2019-10-30
 * @describe 密聊结束弹出框
 */
public class LiveEndCallSheetDialogFragment extends BottomSheetDialogFragment {
    Unbinder unbinder;
    @BindView(R.id.recyclerView)
    TagView mTagView;
    HashMap<String, String> selectMap;
    LiveResultBean liveResultBean;
    MyPersonalBean myPersonalBean;
    @BindView(R.id.dialog_iv_avatar)
    CircleImageView dialogIvAvatar;
    @BindView(R.id.dialog_tv_number)
    TextView dialogTvNumber;
    @BindView(R.id.dialog_tv_city)
    TextView dialogTvCity;
    @BindView(R.id.dialog_tv_price)
    TextView dialogTvPrice;
    @BindView(R.id.dialog_tv_time)
    TextView dialogTvTime;
    @BindView(R.id.dialog_tv_gift_price)
    TextView dialogTvGiftPrice;
    @BindView(R.id.dialog_tv_pay_price)
    TextView dialogTvPayPrice;
    @BindView(R.id.dialog_btn_submit)
    TextView dialogBtnSubmit;
    @BindView(R.id.dialog_tv_nick_name)
    TextView dialogTvNickName;
    String time;
    double giftPrice;
    @BindView(R.id.dialog_rating_bar_score)
    RatingBar dialogRatingBarScore;

    public LiveEndCallSheetDialogFragment(MyPersonalBean myPersonalBean, LiveResultBean liveResultBean, String time, double giftPrice) {
        selectMap = new HashMap<>();
        this.liveResultBean = liveResultBean;
        this.myPersonalBean = myPersonalBean;
        this.time = time;
        this.giftPrice = giftPrice;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);

    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);
        if (liveEndCallSheetDialogFragmentInterface != null) {
            liveEndCallSheetDialogFragmentInterface.dismiss();
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.live_end_call_bottom_sheet_fragment_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        initViews();
        return view;
    }

    private void initViews() {
        //修改评星条的图标颜色
        LayerDrawable layerDrawable = (LayerDrawable) dialogRatingBarScore.getProgressDrawable();
        layerDrawable.getDrawable(2).setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP);
        initUser();
        MainVideoModel.getChannelConfig(2, new ApiModel.CommonCallback<List<ChannelListBean>>() {
            @Override
            public void onSuccess(List<ChannelListBean> channelListBeans) {
                initTags(channelListBeans);
            }

            @Override
            public void onError(String errMsg) {
                ToastUtils.showShort(errMsg);
            }
        });
    }

    private void initUser() {
        if (myPersonalBean == null) {
            return;
        }
        dialogTvNickName.setText(myPersonalBean.getGsName() + "");
        GlideHelper.load(getContext(), myPersonalBean.getHeadimg(), dialogIvAvatar);
        dialogTvNumber.setText("蜜豆号:" + myPersonalBean.getId());
        if (!TextUtils.isEmpty(myPersonalBean.getResidence())) {
            dialogTvCity.setVisibility(View.VISIBLE);
            dialogTvCity.setText(myPersonalBean.getResidence() + "");
        } else {
            dialogTvCity.setVisibility(View.GONE);
        }
        //密聊语音/视频 0 关闭 1 语音 2 视频
        if (myPersonalBean.getSoundsVideoStatus() == 1) {
            dialogTvPrice.setText(myPersonalBean.getVoicePrice() + "蜜豆/分钟");
        } else if (myPersonalBean.getSoundsVideoStatus() == 2) {
            dialogTvPrice.setText(myPersonalBean.getVideoPrice() + "蜜豆/分钟");
        }
        dialogTvTime.setText(time + "");
        dialogTvGiftPrice.setText(giftPrice + "");
        dialogTvPayPrice.setText(liveResultBean.getTotalPrice() + "");
    }

    private void initTags(List<ChannelListBean> channelListBeans) {

        if (channelListBeans == null) {
            return;
        }
        mTagView.removeAllTags();
        for (int i = 0; i < channelListBeans.size(); i++) {
            Tag tag = new Tag(channelListBeans.get(i).getChannelName());
            if (selectMap.get(i + "") != null) {
                tag.tagTextColor = getResources().getColor(R.color.color_font_black);
                tag.layoutColor = getResources().getColor(R.color.color_font_yellow);
                tag.layoutColorPress = getResources().getColor(R.color.color_font_yellow);
            } else {
                tag.tagTextColor = getResources().getColor(R.color.color_font_black);
                tag.layoutColor = getResources().getColor(R.color.bg_gray);
                tag.layoutColorPress = getResources().getColor(R.color.gray);
            }
            tag.radius = 60f;
            tag.tagTextSize = 14f;
            tag.layoutBorderSize = 1f;
            tag.layoutBorderColor = getResources().getColor(R.color.bg_gray);
            tag.isDeletable = false;
            mTagView.addTag(tag);
        }
        mTagView.setOnTagClickListener((position, tag) -> {
            if (selectMap.containsKey(position + "")) {
                selectMap.remove(position + "");
            } else {
                selectMap.put(position + "", channelListBeans.get(position).getId() + "");
            }
            initTags(channelListBeans);
        });
    }

    LiveEndCallSheetDialogFragmentInterface liveEndCallSheetDialogFragmentInterface;

    public void setLiveEndCallSheetDialogFragmentInterface(LiveEndCallSheetDialogFragmentInterface liveEndCallSheetDialogFragmentInterface) {
        this.liveEndCallSheetDialogFragmentInterface = liveEndCallSheetDialogFragmentInterface;
    }

    @OnClick(R.id.dialog_btn_submit)
    public void onViewClicked() {

        if (liveEndCallSheetDialogFragmentInterface != null) {
            liveEndCallSheetDialogFragmentInterface.checked(selectMap, dialogRatingBarScore.getProgress());
        }
    }

    public interface LiveEndCallSheetDialogFragmentInterface {
        void checked(HashMap<String, String> data, int score);

        void dismiss();
    }

}
